<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/4/12
 * Time: 20:45
 */

namespace app\admin\controller\log;


use app\common\controller\Backend;
use app\common\utils\Constant;

/**
 * 导入记录
 *
 * Class Importlog
 * @package app\admin\controller\log
 */
class Importlog extends Backend
{
    protected $model = null;

    public function _initialize(){
        parent::_initialize();
        $this->model = model('ImportLog');
    }

    /**
     * 查看
     */
    public function index()
    {
        $type = $this->request->param('type',0);
        if ($this->request->isAjax())
        {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $userId = $this->auth->id;
            $user = model('User')->where(array('id'=>$userId))->field('user_type,property_id')->find();
            $villageIds = $this->getVillageIds($user['property_id']);
            $map['type'] = $type;
            if($user['user_type'] == Constant::USER_TYPE_PROPERTY) {
                $map['village_id'] = array('in', $villageIds);
            }
            $total = $this->model
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $key=>&$value){
                $value['village_name'] = model('Village')->where(array('id'=>$value['village_id']))->value('name');
                $value['admin_user'] = model('User')->where(array('id'=>$value['admin_id']))->value('nickname');
            }
            unset($value);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        $this->assignconfig('type',$type);
        return $this->view->fetch();
    }
}